package ;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.Lib;
import flash.Memory;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.Vector;
import haxe.Log;


/**
 * ...
 * @author Mike
 */

@:dostuff({
	
	trace( "macro" );
}
)


class Main
{

	private var file:FileReference;
	public static function main() { return new Main(); }
	//static var fileMemory : ByteMemory = new ByteMemory( ( 512 * 512 * 8 ) * 8 );
	//static var bitMapMemory : ByteMemory = new ByteMemory( ( 512 * 512 * 4  )  );
	static var output : Bitmap = new Bitmap( new BitmapData( 1024, 1024, false, 0x00000000 ) );

	//static var  bitMapMemory : ByteMemory = new ByteMemory( ( (512 + 128) * (512+128) )*4, "Map Overview Data" );
	
	public function new()
	{
		haxe.Log.setColor(0xFF0000);
		file = new FileReference();
        configureListeners( file );
		Lib.current.stage.addEventListener( MouseEvent.CLICK, clickedMe); 
		output.x = 400;
		Lib.current.stage.addChild( output );
		var main = new MainSprite();
		flash.Lib.current.addChild(main);
		
	}
	
	function clickedMe( e : Event ) : Void {
        file.browse( getTypes() );
	}
 
	function configureListeners( dispatcher:IEventDispatcher ) : Void {
		dispatcher.addEventListener(Event.CANCEL, cancelHandler);
		dispatcher.addEventListener(Event.COMPLETE, completeHandler);
		//dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
		dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		dispatcher.addEventListener(Event.OPEN, openHandler);
		dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		dispatcher.addEventListener(Event.SELECT, selectHandler);
		dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
	}
	private function getTypes(): Array<FileFilter> {
		var allTypes : Array<FileFilter> = new  Array<FileFilter>();
		allTypes.push( getImageTypeFilter() );
		allTypes.push( getTextTypeFilter() );
		return allTypes;
	}
	private function getImageTypeFilter():FileFilter {
		return new FileFilter("Voxlap (*.vxl)", "*.vxl");
	}
	private function getTextTypeFilter():FileFilter {
		return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
	}
	private function cancelHandler(event:Event):Void {
		trace("cancelHandler: " + event);
	}
	private function uploadCompleteDataHandler(event:DataEvent):Void {
		
	}
	private function ioErrorHandler(event:IOErrorEvent):Void {
		trace("ioErrorHandler: " + event);
	}
	private function openHandler(event:Event):Void {
		 trace("opened : " + event.target.name );
	}	
	/* 
	 * 	Save chunks as they arrive
	 * 
	 */
	private function progressHandler(event:ProgressEvent):Void {

		//event.target.data.readBytes( ba );
	}
	private function securityErrorHandler(event:SecurityErrorEvent):Void {
	}

	private function selectHandler(event:Event):Void {
		trace("size" + event.target.size  );
		file.load();
		file.data;
	}
	private function completeHandler(event:Event):Void {

		//fileMemory = ByteMemory.ofByteArray( event.target.data );
		var map = new Map();
		map.loadmmap( event.target.data , event.target.data.length, output.bitmapData );
		
	}		
	
	
}